// pages/index/index.js
let app = getApp();
import ajax from "../../utils/ajax.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    view_index: 'home',
    now_footer_list_index: 0,
    footer_list: [
      {
        text: '首页',
        icon: '/image/home_footer/a1.png',
        select_icon: '/image/home_footer/a2.png'
      },
      {
        text: '兼职',
        icon: '/image/home_footer/b1.png',
        select_icon: '/image/home_footer/b2.png'
      },
      {
        text: '发布',
        icon: '/image/home_footer/c1.png',
        select_icon: '/image/home_footer/c2.png'
      },
      {
        text: '发现',
        icon: '/image/home_footer/d1.png',
        select_icon: '/image/home_footer/d2.png'
      },
      {
        text: '我的',
        icon: '/image/home_footer/e1.png',
        select_icon: '/image/home_footer/e2.png'
      }
    ],
    city_data: {
      name: '定位中..'
    },
    index_data: {},
    user_info: {},
    home_page:1,//首页列表的加载页码
    

    show_publish_menu:false,
  },
  // 获取首页的职位列表数据
  get_home_job_list(){
    ajax.get({
      url:'fulltime/recommend',
      data:{
        page:this.data.home_page
      }
    }).then(res=>{
      console.log("获取首页的职位列表数据")
      console.log(res)
    })
  },
  switch_publish_menu(){
    this.setData({
      show_publish_menu:!this.data.show_publish_menu
    })
  },
  // 切换页面 
  open_view(e) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    // console.log(e.currentTarget.dataset.index)
    let tag = e.currentTarget.dataset.index;
    this.setData({ now_footer_list_index: tag })
    switch (tag) {
      case 0:
        this.setData({
          view_index: "home"
        })
        wx.setNavigationBarTitle({
          title: '乐投招聘'
        })
        break;
      case 1:
        wx.navigateTo({
          url: '../part_time/index/index',
        })
        // this.setData({
        //   view_index: "zhiwei"
        // })
        // wx.setNavigationBarTitle({
        //   title: '职位'
        // })
        break;
      case 2:
        this.switch_publish_menu()

        break;
      case 3:
        this.setData({
          view_index: "faxian"
        })
        wx.setNavigationBarTitle({
          title: '发现'
        })
        break;
      case 4:
        this.setData({
          view_index: "wode"
        })
        wx.setNavigationBarTitle({
          title: '个人中心'
        })
        break;
    }
  },
  // 选择城市页面选择后出发此方法
  updata_city_data(city_data) {
    // console.log("index updata_city_data");
    // console.log(city_data)
    this.setData({
      city_data: city_data
    })
    app.common.now_city = city_data
  },
  // index 页面加载时 触发一次 定位当前位置的城市
  get_location() {
    app.get_location_to_city().then(res => {
      this.setData({
        city_data: res.data
      })
      app.common.now_city = res.data;
    })
  },

  init_index_data() {
    let index_data = this.data.index_data;
    // 获取banner
    // console.log("获取banner");
    ajax.get({ url: 'lunbo_images' }).then(res => {
      // console.log(res);
      index_data.banner = res.data;
      this.setData({ index_data })
    })
    // console.log("获取首页数据");
    ajax.get({ url: "home_data" }).then(res => {
      // console.log(res);
      index_data.home_data = res.data;
      this.setData({ index_data })
    })

    ajax.get({ url: "home_notice" }).then(res => {
      // console.log(res);
      index_data.home_notice = res.data;
      this.setData({ index_data })
    })

    ajax.get({ url: "company_recommend" }).then(res => {
      // console.log(res);
      index_data.company_recommend = res.data;
      this.setData({ index_data })
    })

  },
  // 更新用户信息
  update_user_info() {
    // console.log("update_user_info");
    // console.log(app.common.token)
    if (app.common.token) {
      ajax.get({ url: 'user_info' }).then(res => {
        // console.log("更新用户信息");
        // console.log(res.data);
        if (res.code == 0) {
          app.common.user_info = res.data;
          this.setData({
            user_info: res.data
          })
          app.toast("信息已更新")
        }
      })
    } else {
      let user_info = {
        headimgurl: 'http://img.gek6.com/FnzCBkjI6ByH69cvpiYS8XMdXytx',
        nickname: '未登录',
        no_login:true
      }
      app.common.user_info = user_info
      this.setData({
        user_info
      })
    }
    return;

  },
  call_kefu(){
    
  },
  onLoad: function (options) {
    this.init_index_data();
    this.get_location();
    
    this.get_home_job_list()
  },
  onReady: function () {

  },
  onShow: function () {
    // this.setData({
    //   view_index: "wode"
    // })
    this.update_user_info();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("onPullDownRefresh")
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000);
    let view_index = this.data.view_index;
    if (view_index == "home") {
      console.log("刷新首页数据");

    }
    if (view_index == "zhiwei") {
      console.log("刷新职位数据");

    }
    if (view_index == "faxian") {
      console.log("刷新发现数据");

    }

    if (view_index == "wode") {
      console.log("刷新我的数据");
      setTimeout(() => {
      this.update_user_info();
        
      }, 800);
    }


  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("onReachBottom")
    let view_index = this.data.view_index;
    if (view_index == "home") {
      console.log("加载首页数据");

    }
    if (view_index == "zhiwei") {
      console.log("加载职位数据");

    }
    if (view_index == "faxian") {
      console.log("加载发现数据");

    }

    if (view_index == "wode") {
      console.log("加载我的数据");

    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})